//
//  ZYCategorySearchViewController.m
//  Adorable_Things
//
//  Created by huzhiyan on 16/1/18.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "ZYCategorySearchViewController.h"
#import "ZYCategorySearchCollectionViewCell.h"
#import "ZYCategoryHeaderCollectionReusableView.h"
#import "ZYCategorySearchModel.h"
#import "ZYCategoryHeaderView.h"
#import "DJSearchViewController.h"


@interface ZYCategorySearchViewController () <UICollectionViewDataSource,UICollectionViewDelegate,UISearchBarDelegate>

@property (nonatomic, strong) NSMutableArray *dataModelArray;
@property (nonatomic, strong) ZYCategoryHeaderView *headerView;


@end

@implementation ZYCategorySearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    [self addSubHeaderView];
    [self addSubCollectionViewItemSize];
    [self setUpData];
}

#pragma mark ---  添加导航自定义视图
// 添加导航自定义视图
- (void)addSubHeaderView
{
    _headerView = [[ZYCategoryHeaderView alloc] initWithFrame:CGRectMake(0, 20, kWidth, 45)];
    _headerView.backgroundColor = [UIColor whiteColor];
    [_headerView.backItemButton addTarget:self action:@selector(backItemButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_headerView];
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(55, 2, kWidth - 110, 40)];
    searchBar.delegate = self;
    searchBar.searchBarStyle = UISearchBarStyleMinimal;
    
    [_headerView addSubview:searchBar];
}

- (void)backItemButtonClick:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark --- search代理方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    DJSearchViewController *searchVC = [[DJSearchViewController alloc] init];
    searchVC.name = searchBar.text;
    [self.navigationController pushViewController:searchVC animated:YES];

}


#pragma mark ---  添加collectionView
- (void)addSubCollectionViewItemSize
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView.collectionViewLayout = flowLayout;
    flowLayout.itemSize = CGSizeMake((kWidth - 60) / 5, 30);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 20, 10);
    flowLayout.headerReferenceSize = CGSizeMake(kWidth, 30);
    [self.collectionView registerClass:[ZYCategorySearchCollectionViewCell class] forCellWithReuseIdentifier:@"ZYCategorySearchCollectionViewCell"];
    [self.collectionView registerClass:[ZYCategoryHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZYCategoryHeaderCollectionReusableView"];
    
}

#pragma mark --- 请求数据
- (void)setUpData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:@"http://api.menghuoapp.com/v1/search/hot" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *dataArray = dic[@"data"];
        _dataModelArray = [NSMutableArray array];
        for (NSDictionary *dataDic in dataArray) {
            ZYCategorySearchModel *model = [[ZYCategorySearchModel alloc] init];
            [model setValuesForKeysWithDictionary:dataDic];
            [_dataModelArray addObject:model];
        }
        [_collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataModelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZYCategorySearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZYCategorySearchCollectionViewCell" forIndexPath:indexPath];
    ZYCategorySearchModel *model = _dataModelArray[indexPath.row];
    cell.nameLbel.text = model.word;
    [cell itemColorSelected:[model.highlight floatValue]];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        ZYCategoryHeaderCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZYCategoryHeaderCollectionReusableView" forIndexPath:indexPath];
        return headerView;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZYCategorySearchModel *model = _dataModelArray[indexPath.row];
    DJSearchViewController *searchVC = [[DJSearchViewController alloc] init];
    searchVC.name = model.word;
    if ([model.word isEqualToString:@"饿了"]) {
        searchVC.name = @"饼干";
    }
    [self.navigationController pushViewController:searchVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
